#include <sys/signal.h>
#include <event.h>

/*回调函数，特定信号发生时，会调用这个函数*/
void signal_cb(int fd, short event, void* argc)
{
    /*event_base 用于管理所有的事件和回调*/
    struct event_base* base = (event_base*)argc;
    /*两秒 + 0微秒*/
    struct timeval delay = {2, 0};
    printf("Caught an interrupt signal; exiting cleanly in two seconds...\n");
    /*请求libevent的事件循环在指定的延迟后退出*/
    event_base_loopexit(base, &delay);
}

void timeout_cb(int fd, short event, void* argc)
{
    printf("timeout\n");
}

int main()
{
    struct event_base* base = event_init();
    struct event* signal_event = evsignal_new(base, SIGINT, signal_cb, base);
    event_add(signal_event, NULL);

    timeval tv = {1, 0};
    struct event* timeout_event = evtimer_new(base, timeout_cb, NULL);
    event_add(timeout_event, &tv);

    event_base_dispatch(base);

    event_free(timeout_event);
    event_free(signal_event);
    event_base_free(base);
    return 0;
}